from django.db import models
from django.contrib.auth.models import User
from business.models import Partner, Workplace
from business.models import Vat
from inventory.models import Item, Drug


class PurchaseInvoice(models.Model):
    created = models.DateTimeField(auto_now_add=True, editable=False)
    updated = models.DateTimeField(auto_now=True, editable=False)
    created_by = models.ForeignKey(User, editable=False)
    workplace = models.ForeignKey(Workplace, editable=False)

    issued = models.DateField('Emisa', help_text='Data emiterii')
    due = models.DateField('Scadenta', help_text='Data scadentei')
    issuer = models.ForeignKey(Partner,
        verbose_name='Emitent',
        help_text='Partenerul care a emis factura')
    series = models.CharField('Serie',
        max_length=10,
        help_text='Seria facturii')
    number = models.DecimalField('Numar',
        max_digits=10,
        decimal_places=0,
        help_text='Numarul facturii')
    comment = models.TextField('Comentariu', blank=True,
        help_text='Memento (optional)')

    def __unicode__(self):
        return ' '.join(self.series, str(self.number))

    class Meta:
        unique_together = (('issuer', 'series', 'number'),)


class SalesInvoice(models.Model):
    created = models.DateTimeField(auto_now_add=True, editable=False)
    updated = models.DateTimeField(auto_now=True, editable=False)
    created_by = models.ForeignKey(User, editable=False)
    updated_by = models.ForeignKey(User, editable=False)
    workplace = models.ForeignKey(Workplace, editable=False)

    # TODO: Adauga functia de citire cu barcode personalizat
    # IDEE: foloseste aceeasi tehnologie ca reteta electronica:
    # adica, Matrix cu XML codat base64 si arhivat ZIP

    issued = models.DateField('Emisa', help_text='Data emiterii')
    due = models.DateField('Scadenta', help_text='Data scadentei')
    issuer = models.ForeignKey(Partner,
        verbose_name='Emitent',
        help_text='Punctul de lucru care a emis factura')
    series = models.CharField('Serie',
        max_length=10,
        help_text='Seria facturii')
    number = models.DecimalField('Numar',
        max_digits=10,
        decimal_places=0,
        help_text='Numarul facturii')
    has_receipt = models.NullBooleanField('a fost emisa chitanta?',
        help_text='Factura a fost emisa impreuna cu chitanta?')
    comment = models.TextField('Comentariu', blank=True,
        help_text='Memento (optional)')

    def __unicode__(self):
        return ' '.join(self.series, str(self.number))

    class Meta:
        unique_together = (('issuer', 'series', 'number'),)


class InvoiceItem(models.Model):
    created = models.DateTimeField(auto_now_add=True, editable=False)
    updated = models.DateTimeField(auto_now=True, editable=False)
    created_by = models.ForeignKey(User, editable=False)
    updated_by = models.ForeignKey(User, editable=False)

    # The following two are mutually exclusive:
    item = models.ForeignKey(Item,
        verbose_name='Produs',
        help_text='Produsul care apare in factura',
        null=True)
    drug = models.ForeignKey(Drug,
        verbose_name='Medicament',
        help_text='Medicamentul care apare in factura',
        null=True)

    vat = models.ForeignKey(Vat, verbose_name='TVA', help_text='Cota TVA')
